//
//  SHPWelcomeViewController.swift
//  weibo
//
//  Created by songhuipan on 16/4/20.
//  Copyright © 2016年 songhuipan. All rights reserved.
//

import UIKit
import SDWebImage
class SHPWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = backImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

    }
    // MARK:界面显示完成后在执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)
        setWelcomAnimation()
    }
/// 设置欢迎界面的动画
    func setWelcomAnimation (){
        // 改变约束
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
        }
        // 创建动画
        UIView.animateWithDuration(2, delay: 0.25, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //头像移动动画结束后，展现文字
              UIView.animateWithDuration(0.25, animations: { () -> Void in
                self.welcomeLabel.alpha = 1
                }, completion: { (_) -> Void in
                    
            NSNotificationCenter.defaultCenter().postNotificationName("switchDefaultController", object: "welcome")
              })
                
        }
    }
/// 设置欢迎界面UI视图
    func setupUI () {
        
        view.addSubview(iconImageView)
        view.addSubview(welcomeLabel)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(400)
            make.size.equalTo(CGSizeMake(85, 85))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconImageView.snp_bottom).offset(16)
        }
    }
    /// 懒加载文字控件
    private lazy var welcomeLabel:UILabel = {
        
        let welcomeLabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 17)
        
        welcomeLabel.textAlignment = .Center
        
        welcomeLabel.text = "欢迎回来"
        
        welcomeLabel.alpha = 0
        
        return welcomeLabel
        
    }()
    /// 懒加载背景图片控件
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    /// 懒加载头像控件
    private lazy var iconImageView:UIImageView = {
        let iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconImageView.layer.cornerRadius = 85/2
        iconImageView.layer.borderColor = ThemeColor.CGColor
        iconImageView.layer.borderWidth = 2
        iconImageView.clipsToBounds = true
        // 注意这里的URL是必选值
        iconImageView.sd_setImageWithURL(NSURL(string: SHPUserAccountViewModel.sharedUserAccount.account?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        return iconImageView
        
    }()
    
}
